#!/bin/bash 放置在远程服务docker目录下，即：DOCKER_DIR

# 设置目标目录和服务器名称
DOCKER_DIR="/home/agilerobots/docker/"
BOOT_TARGET_DIR="${DOCKER_DIR}java/boot"
GATEWAY_TARGET_DIR="${DOCKER_DIR}java/gateway"
BOOT_SERVER_NAME="boot"
GATEWAY_SERVER_NAME="gateway"

# 查找最新的 boot JAR 文件
BOOT_JAR_PATTERN="${BOOT_TARGET_DIR}/${BOOT_SERVER_NAME}*.jar"
LATEST_BOOT_JAR=$(ls -t $BOOT_JAR_PATTERN 2>/dev/null | head -n 1)

# 提取 boot 版本号
BOOT_VERSION=$(basename "$LATEST_BOOT_JAR" .jar | sed "s/^${BOOT_SERVER_NAME}-//")

# 查找最新的 gateway JAR 文件
GATEWAY_JAR_PATTERN="${GATEWAY_TARGET_DIR}/${GATEWAY_SERVER_NAME}*.jar"
LATEST_GATEWAY_JAR=$(ls -t $GATEWAY_JAR_PATTERN 2>/dev/null | head -n 1)

# 提取 gateway 版本号
GATEWAY_VERSION=$(basename "$LATEST_GATEWAY_JAR" .jar | sed "s/^${GATEWAY_SERVER_NAME}-//")

# 创建 .env 文件
echo "BOOT_VERSION=$BOOT_VERSION" > "${DOCKER_DIR}.env"
echo "GATEWAY_VERSION=$GATEWAY_VERSION" >> "${DOCKER_DIR}.env"